(function($){
    var total = 0;
    function initChecked() {
        var ids = paramTags.split(',');
        $('#bp_tags_layer li').each(function(){
            var obj = $(this);
            var id = obj.attr('did');
            for(var i=0; i<ids.length; i++) {
                if(id == ids[i]) {
                    obj.attr('c', '1');
                }
            }
        });
        totalChecked();
    }
    function checkedItem() {
        var obj = $(this);
        var checked = obj.attr('c');
        var v = checked == '1' ? '0' : '1';
        if(v >= '1' && total >= 6) {
            layer.msg('最多只能选择六个标签！', {icon: 2});
        } else {
            obj.attr('c', v);
            totalChecked();
        }
    }
    function setCheckedHtml(obj, checked) {
        if(checked) {
            total += 1;
            obj.addClass('checked');
            obj.find('i').removeClass('fa-square-o').addClass('fa-check-square-o');
        } else {
            obj.removeClass('checked');
            obj.find('i').removeClass('fa-check-square-o').addClass('fa-square-o');
        }
    }
    function totalChecked() {
        total = 0;
        $('#bp_tags_layer li').each(function(){
            var obj = $(this);
            var checked = obj.attr('c');
            setCheckedHtml(obj, checked == '1');
        });
    }

    function isContained(a, b){
        if(!(a instanceof Array) || !(b instanceof Array)) return false;
        if(a.length < b.length) return false;

        for(var i = 0, len = b.length; i < len; i++){
            if(a.indexOf(b[i]) == -1) return false;
        }
        return true;
    }
    function isRepeat(arr){
        var hash = {};
        for(var i in arr) {
            if(hash[arr[i]]){
                return true;
            }
            hash[arr[i]] = true;
        }
        return false;
    }


    function save() {
        var ids = [];
        var values = [];
        $('#bp_tags_layer li').each(function(){
            var obj = $(this);
            var checked = obj.attr('c');
            if(checked == '1') {
                ids.push(obj.attr('did'));
                values.push(obj.attr('dvalue'));
            }
        });

        ids = ids.join(',');
        values = values.join(',');
        !!callback && eval(callback + "(\"" + ids + "\", \"" + values + "\")");



/*        var arr = [],data={},m=[],
            rs = $('#text-value').val().split(',');
        $('#bp_tags_layer li').each(function(){
            var obj = $(this);
            for (var i =0;i<obj.length;i++) {
                arr.push(obj.attr('did'));
                m.push(obj.attr('dvalue'));
            }

            for (var i =0;i<arr.length;i++) {
                data[arr[i]] = m[i];
            }

        });


        if (!isContained(arr,rs) || isRepeat(rs)) {
            layer.msg('标签不存在！', {icon: 2});
            $('#text-value').val('');
        } else {
            var ids = [];
            var values = [];
            ids = rs.join(',');
            for (var i =0;i<rs.length;i++) {
                if(data[rs[i]]) {
                    values.push(data[rs[i]])
                }
            }
            values = values.join(',');

            !!callback && eval(callback + "(\"" + ids + "\", \"" + values + "\")");
        }*/

    }
    $(function(){

        initChecked();
        $('#bp_tags_layer li').on('click', checkedItem);
        $('#but_ok').on('click', save);
    });
})(jQuery);